/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFVisual
  \brief event (2D) visualization
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-11-13
  \date 2021-12-11 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-11-16   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <TGeoManager.h>
#include <TGeoMaterial.h>
#include <TGeoMatrix.h>
#include <TGeoMedium.h>
#include <TClonesArray.h>
#include <TCanvas.h>
#include <TGraph.h>
#include <TFile.h>
#include "FairRootManager.h"
#include "FairRootFileSink.h"
#include "ETFVisual.h"
#include "ETFDetectorManager.h"
#include "ETFTrackManager.h"
#include "ETFPID.h"
#include "ETFPIDManager.h"

ClassImp(ETFVisual);

ETFVisual *ETFVisual::fInstance = nullptr;
double ETFVisual::kL = 10000.;
double ETFVisual::kW = 3200.;

ETFVisual::ETFVisual(){
  Configure();
  // ConfigureGeom();
} // end ctor

ETFVisual::~ETFVisual(){}

ETFVisual *ETFVisual::Instance(){
  if(!fInstance) fInstance = new ETFVisual();
  return fInstance;
} // end member function Instance

void ETFVisual::AddNode(TGeoVolume *vol, int copy_no, TGeoMatrix *mat){
  fTop->AddNode(vol, copy_no, mat);
} // end member function AddNode

void ETFVisual::ConfigureGeom(){
  fGeom = new TGeoManager("ETFVisual", "ETF visualization");
  TGeoMaterial *matVacuum = new TGeoMaterial("vacuum", 0., 0., 0.);
  TGeoMedium *vacuum = new TGeoMedium("vacuum", 1, matVacuum);
  // x: -1000., 3200., y: -600., 600., z: -2500., 8500.
  fTop = fGeom->MakeBox("TOP", vacuum, 2100., 600., 5500.);
  fGeom->SetTopVolume(fTop);
} // end member function ConfigureGeom

void ETFVisual::Configure(){
  static FairRootManager *man = FairRootManager::Instance();
  fRootFile = dynamic_cast<FairRootFileSink *>(man->GetSink())->GetRootFile();
  if(!fRootFile->FindObjectAny("VISUAL")) fRootFile->mkdir("VISUAL");
  fCanvas = new TCanvas("ETFAna-VISUAL", "ETFAna-VISUAL", 800, 600);

  fGMainFrame = new TGraph();
  fGMainFrame->SetNameTitle("MainFrame", "Experiment Snap");
  // Experiment visualization canvas size
  // (-W, W)       0                 (L, W)
  //
  // (-W,-W) 	    0                 (L,-W)
	fGMainFrame->SetPoint(0, -kW,  kW);       fGMainFrame->SetPoint(1,  kL,  kW);
	fGMainFrame->SetPoint(3, -kW, -kW+2000.); fGMainFrame->SetPoint(2,  kL, -kW+2000.);
  fGMainFrame->SetPoint(4, -kW,  kW); // this point seals the box
} // end member function Configure

// \param index: event index
void ETFVisual::DrawEventSnap(int index){
  char name[64], title[128];
  sprintf(name, "Snap#%d", index);
  sprintf(title, "Experiment Snap - Event#%d", index);
  fCanvas->SetName(name); fCanvas->SetTitle(title);
  fGMainFrame->SetTitle(title);
  fGMainFrame->Draw("APL");
  // draw all the detecting units with highlighted hits
  ETFDetectorManager::Instance()->Draw(); // draw the det hits
  ETFTrackManager::Instance()->Draw(); // draw the tracks
  // draw the arc (trajectory) in the dipole magnet
  ETFPIDManager::Instance()->GetPID()->Draw();

  fRootFile->cd("VISUAL");
  fCanvas->Write("", TObject::kOverwrite);
  fRootFile->cd(); // exit to the root folder
} // end member function DrawEventSnap
